package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hx_Alex on 2018/5/26.
 */

public class ClearAllPageReceiver extends BaseReceiver {
    private AccessibilityNodeInfo nodeInfo;
    private boolean isClickOperaMenu = false;

    public ClearAllPageReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="清空所有页面命令";
    }

    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }

    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }


    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("ClearAllPageReceiver","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
        searchManger.searchMorePageButtonInfo(nodeInfo);
        AccessibilityNodeInfo morePageButtonInfo = searchManger.getMorePageButtonInfo();
        AccessibilityNodeInfo closeAllBtuutonInfo = searchManger.getCloseAllBtuutonInfo();
        if (morePageButtonInfo != null) {
            if (!isClickOperaMenu ) {
                
                if(searchManger.performActionClick(morePageButtonInfo))
                {
                    isClickOperaMenu = true;
                }else
                {
                    isClickOperaMenu=false;
                }
               
            } else {
                searchManger.searchCloseAllBtuutonInfo(nodeInfo);
            }

            if (closeAllBtuutonInfo != null) {
                Timer timer=new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        boolean b = searchManger.performActionClick(closeAllBtuutonInfo);
                        if (b) {
                            Log.e("updateWebSiteReceiver", "关闭页面结果 reslut:" + b);
                        } else {
                            Log.e("updateWebSiteReceiver", "关闭页面失败");
                            searchManger.setMorePageButtonInfo(null);
                            searchManger.setCloseAllBtuutonInfo(null);
                        }
                    }
                },500);
                commandCompleteListener.commandCompleteListener();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
